import { GuideConfig } from './../define/GuideConfig';
import { ShowGuideData } from './../define/GuideData';
import Boot, { AuditLevel } from "../../../../Boot";
import { Loger } from "../../../../framework/loger/Loger";
import Utils from "../../../../framework/utils/Utils";
import ViewModelBase from "../../../../framework/view/ViewModelBase";
import { GuideData } from "../define/GuideData";
import { GameGuideEvent } from "../define/GuideEvents";
import * as cc from "cc";
import VCGuide from "../vc/VCGuide";


export default class ModelGuide extends ViewModelBase {
    static modelName: string = "ModelGuide";

    /**上一个完成的引导id */
    private _lastEndStep: number = 0;
    /**上一个完成的引导id */
    public get lastEndStep(): number { return this._lastEndStep }
    /**当前正在播放的引导id  */
    private _showingGuide: number = 0;
    /**当前正在播放的引导id  */
    public get showingGuide(): number { return this._showingGuide }
    /**是否正在播放引导 */
    public get isShowing(): boolean { return this._showingGuide > 0 }

    private guideConfig:{[key:number]:GuideData} = null;
    initData(data:{[key:number]:GuideData}){
        this.guideConfig = data;
    }
    
    tryEnter(params?: { [key: string]: any, hideOther?: boolean, unHideNames?: string[] }){
        if (!this.checkEnable()) {
            return;
        }
        this.initData(GuideConfig)
        this.showView(VCGuide, params)
    }

    checkEnable(){
        return true;
    }

    clear() {
        this._lastEndStep = 0;
    }

    /**判断是不是引导第一步 */
    public checkFirstGuide(): boolean {
        return this.isInGuide() && this._lastEndStep == 0;
    }

    /**判断是不是还在引导中 */
    public isInGuide(): boolean {
        return Boot.auditLevel < AuditLevel.High && this._lastEndStep != -1;
    }

    /**
     * 检查引导是否能开启
     * @param id 引导步骤
     */
    public checkCanGuide(id: number): boolean {
        Loger.log("check guide id ------>>>", id)
        if (this._lastEndStep == null || Boot.auditLevel > AuditLevel.Low) {
            return false;
        }
        Loger.log("last end step ------>>>", this._lastEndStep)
        return this._lastEndStep != -1 && id >= this._lastEndStep + 1;
    }

    /**尝试开启新手引导 */
    public tryGuide(id: number, target?: cc.Node, callback?: (isGuide: boolean) => void, delayTime: number = 0): boolean {
        if (!this.checkCanGuide(id) || this._showingGuide == id) {
            if (callback) {
                callback(false);
            }
            return false;
        }
        this._showingGuide = id;
        if (delayTime > 0) {
            this.blockAll();
            Utils.delay(delayTime).then(() => {
                this._showGuide(id, target, callback);
            })
        } else {
            this._showGuide(id, target, callback);
        }
        return true;
    }

    /**展示一次性引导 */
    public showOnceGuide(target: cc.Node, data: GuideData,callback?: (isGuide: boolean) => void) {
        if (Boot.auditLevel > AuditLevel.Normal) {
            return;
        }
        const isPassClick = callback == null ? true : false;
        const showGuideData:ShowGuideData={
            guideData:data,
            isPassClick:isPassClick,
            target:target,
            callback:()=>{
                callback && callback(true);
            }
        }
        GameGuideEvent.ON_GUIDE_SHOW.emit(showGuideData);
    }

    private _showGuide(id: number, target?: cc.Node, callback?: (isGuide: boolean) => void) {
        const isPassClick = callback == null ? true : false;
        const showGuideData:ShowGuideData={
            guideData:this.getGuideData(id),
            isPassClick:isPassClick,
            target:target,
            callback:()=>{
                this.setEndGuide(id);
                callback && callback(true);
            }
        }
        GameGuideEvent.ON_GUIDE_SHOW.emit(showGuideData);
    }

    public setEndGuide(id: number) {
        if (!this.checkCanGuide(id)) { return }
        this._showingGuide = 0;
        this._lastEndStep = id;
        if (!this.getGuideData(this._lastEndStep + 1)) {
            Loger.log("set end guide last id ------>>>>", id)
            this._lastEndStep = -1;
        }
        GameGuideEvent.ON_GUIDE_STEP_CHANGE.emit(this._lastEndStep);
    }

    public setGuideDone() {
        this._lastEndStep = -1;
        GameGuideEvent.ON_GUIDE_STEP_CHANGE.emit(this._lastEndStep);
    }

    public setLastEndStep(step:number){
        this._lastEndStep = step;
        GameGuideEvent.ON_GUIDE_STEP_CHANGE.emit(this._lastEndStep);
    }

    public getGuideData(id: number): GuideData {
        return this.guideConfig[id]
    }


    public blockAll(){
        GameGuideEvent.ON_BLOCK_ALL.emit();
    }

    public passAll(){
        GameGuideEvent.ON_PASS_ALL.emit();
    }

}
